package dialogs;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.HashMap;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeListener;

import panels.CosysmoverviewPanel;

import main.COINCOMO;
import main.GlobalMethods;
import main.Icons;

import core.COINCOMOComponent;
import core.COINCOMOSubSystem;
import database.COINCOMOComponentManager;
import database.COINCOMOSubSystemManager;

public class CotTailoringCodeEAFPanel extends JPanel implements ActionListener {

	DecimalFormat format1Decimal = new DecimalFormat( "0.0" );
	DecimalFormat format2Decimals = new DecimalFormat( "0.00" );
	//Arnab
	private JLabel[] tailoringLabels1 = new JLabel[]
	                                               {
			new JLabel( "<html><body style='text-align:center'>Parameter<br />Specification<br /></body></html>" ) , new JLabel( "<html><body style='text-align:center'>Script<br />Writing<br /></body></html>" )
	                                               };

	private JLabel[] tailoringLabels2 = new JLabel[]
	                                               {
			new JLabel( "<html><body style='text-align:center'>I/O Report<br />Spec & Layout<br /></body></html>"), new JLabel( "<html><body style='text-align:center'>GUI Screen<br />Spec & Layout<br /></body></html>" )
	                                               };

	private JLabel[] tailoringLabels3 = new JLabel[]
	                                               {
			new JLabel( "<html><body style='text-align:center'>Security/Access<br />Init & Setup<br /></body></html>") , new JLabel( "<html><body style='text-align:center'>Availibility<br />COTS Tail Tools<br /></body></html>")
	                                               };



	private JButton[] tailoringButtons1 = new JButton[]
	                                                  {
			new JButton( "NOM" ) , new JButton( "NOM" ) 
	                                                  };

	private JButton[] tailoringButtons2 = new JButton[]
	                                                  {
			new JButton( "NOM" ) , new JButton( "NOM" ) 
	                                                  };

	private JButton[] tailoringButtons3 = new JButton[]
	                                                  {
			new JButton( "NOM" ) , new JButton( "NOM" )  
	                                                  };


	private JLabel[] driverLabels = new JLabel[]
	                                           {
			new JLabel( "Tailoring Labels Part 1" ) , new JLabel( "Tailoring Labels Part 2" ) , new JLabel( "Tailoring Labels Part 3" )  
	                                           };

	private JLabel[][] driverSetLabels = new JLabel[][]
	                                                  {
			tailoringLabels1 ,
			tailoringLabels2 , 
			tailoringLabels3 , 
	                                                  };

	private JButton[][] ratingSetButtons = new JButton[][]
	                                                     {
			tailoringButtons1 ,
			tailoringButtons2 ,
			tailoringButtons3 ,
	                                                     };


	JPopupMenu ratingPopup = new JPopupMenu();

	JPopupMenu incrementPopup = new JPopupMenu();

	JMenuItem vloMenuItem = new JMenuItem( "VLO" );

	JMenuItem loMenuItem = new JMenuItem( "LO" );

	JMenuItem nomMenuItem = new JMenuItem( "NOM" );

	JMenuItem hiMenuItem = new JMenuItem( "HI" );

	JMenuItem vhiMenuItem = new JMenuItem( "VHI" );

	JButton applyButton = new JButton( "Apply" );

	JButton resetButton = new JButton( "Reset" );

	JButton closeButton = new JButton( "Close" );
	// The Button From Which the Popup Generated ...
	JButton ownerButton = null;
	
	JTextField numOfCots = new JTextField("0");
	
	JTextField medianTailEffort = new JTextField();

	COINCOMOComponent component = null;

	// For Updating the Table
	int id = 0 ;

	/**
	 *
	 * @param frame to be used to set the Owner of the Dialog
	 * @param subComponent object to read from and save to
	 * @param rowNumber to update the corresponding CLEF's SubComponent
	 */

	public CotTailoringCodeEAFPanel( JFrame frame , COINCOMOComponent component , int id )
	{

		this.id = id ;
		this.component = component;


		GlobalMethods.updateStatusBar( "Done." );

		// Popup
		ratingPopup.add( vloMenuItem );
		ratingPopup.add( loMenuItem );
		ratingPopup.add( nomMenuItem );
		ratingPopup.add( hiMenuItem );
		ratingPopup.add( vhiMenuItem );


		// Menu Items
		vloMenuItem.addActionListener( this );
		loMenuItem.addActionListener( this );
		nomMenuItem.addActionListener( this );
		hiMenuItem.addActionListener( this );
		vhiMenuItem.addActionListener( this );


		this.setLayout( null );

		int ratings[] = component.getRatings() ;
		int counter = 0 ;

		if(component.getLoadedTailCocotsRatings().containsKey(new Integer(id)))
			loadParameters();

		// GUI
		for ( int i = 0 ; i < driverSetLabels.length ; i++ )
		{
			// Legend
			TitledBorder legendTitleBorder = BorderFactory.createTitledBorder( driverLabels[i].getText() );
			legendTitleBorder.setTitleColor( Color.BLUE );

			this.add( driverLabels[i] );

			driverLabels[i].setText( "" );
			driverLabels[i].setBorder( legendTitleBorder );
			driverLabels[i].setFont( new Font( "courier" , 1 , 12 ) );


			driverLabels[i].setBounds( 10 , 10 + i * 100 , 530 , 100 );


			JLabel label1 = new JLabel( "Rating" );
			// JLabel label2 = new JLabel( "% Incr" );

			label1.setFont( new Font( "arial" , 1 , 11 ) );
			//  label2.setFont( new Font( "arial" , 1 , 11 ) );

			this.add( label1 );
			//  this.add( label2 );


			label1.setBounds( 40 , 10 + i * 100 + 45 , 65 , 25 );
			//     label2.setBounds( 40 , 10 + i * 100 + 65 , 65 , 25 );


			for ( int j = 0 ; j < driverSetLabels[i].length ; j++ )
			{
				this.add( driverSetLabels[i][j] );
				this.add( ratingSetButtons[i][j] );


				ratingSetButtons[i][j].setName( driverSetLabels[i][j].getText() );

				if(!component.getLoadedCocotsRatings().containsKey(new Integer(id)))
					ratingSetButtons[i][j].setText( GlobalMethods.convertToRating( ratings[counter] ) ) ;
				//incrementSetButtons[i][j].setText( GlobalMethods.convertToPercent( percent[counter++] ) ) ;

				ratingSetButtons[i][j].setFont( new Font( "courier" , 1 , 16 ) );
				//incrementSetButtons[i][j].setFont( new Font( "courier" , 1 , 16 ) );

				ratingSetButtons[i][j].setFocusable( false );
				//incrementSetButtons[i][j].setFocusable( false );

				ratingSetButtons[i][j].addActionListener( this );
				//incrementSetButtons[i][j].addActionListener( this );

				driverSetLabels[i][j].setHorizontalAlignment( JLabel.CENTER );
				driverSetLabels[i][j].setFont( new Font( "arial" , 1 , 11 ) );


				driverSetLabels[i][j].setBounds( 90 + j * 150 , 10 + i * 100 + 20 , 90 , 25 );
				ratingSetButtons[i][j].setBounds( 90 + j * 150 , 10 + i * 100 + 45 , 90 , 25 );
				//incrementSetButtons[i][j].setBounds( 90 + j * 70 , 10 + i * 100 + 65 , 65 , 25 );

				
				
				
				
				

			}
			
			if(!component.getLoadedCocotsRatings().containsKey(new Integer(id)))
			{
				COINCOMOComponentManager.loadMedianDefault(id, component);
				medianTailEffort.setText(""+component.getTailMedianDefault());
			}
			
			this.add(numOfCots);
			this.add(medianTailEffort);
			
			JLabel labelNumOfCots = new JLabel( "<html><body style='text-align:center'>Number of<br />Cots<br /></body></html>" );
			labelNumOfCots.setHorizontalAlignment( JLabel.CENTER );
			labelNumOfCots.setFont( new Font( "arial" , 1 , 11 ) );
			this.add( labelNumOfCots );
			
			JLabel labelMedianTailEffort = new JLabel( "<html><body style='text-align:center'>Median Tail<br />Effort<br /></body></html>" );
			labelMedianTailEffort.setHorizontalAlignment( JLabel.CENTER );
			labelMedianTailEffort.setFont( new Font( "arial" , 1 , 11 ) );
			this.add( labelMedianTailEffort );
			
			labelNumOfCots.setBounds(90 + driverSetLabels[i].length * 150 , 10 + 0 * 100 + 20 , 90 , 25);
			numOfCots.setBounds(90 + driverSetLabels[i].length * 150 , 10 + 0 * 100 + 45 , 90 , 25);
			
			labelMedianTailEffort.setBounds(90 + driverSetLabels[i].length * 150 , 10 + 1 * 100 + 20 , 90 , 25);
			medianTailEffort.setBounds(90 + driverSetLabels[i].length * 150 , 10 + 1 * 100 + 45 , 90 , 25);
			
			numOfCots.addActionListener(this);
			medianTailEffort.addActionListener(this);
			
		}



		this.setSize( 570 , 550);
		this.setVisible( true );

	}

	public void loadParameters()
	{
		
		HashMap<Integer, double[]> loadedParameters = component.getLoadedTailCocotsRatings();
		//HashMap<Integer, int[]> loadedParameters = subSystem.getLoadedCocotsRatings();
		if( loadedParameters.size() > 0 )
		{
			double[] ratings = loadedParameters.get(id);
			int counter = 0;

			for ( int i = 0 ; i < ratingSetButtons.length ; i++ )
			{
				for ( int j = 0 ; j < ratingSetButtons[i].length ; j++ )
				{
					((JButton) ratingSetButtons[i][j]).setText( GlobalMethods.convertIntegerToRating(ratings[counter++]) );
					//percent[counter++] = GlobalMethods.convertFromPercent( ((JButton) incrementSetButtons[i][j]).getText() );
				}
			}
			medianTailEffort.setText( "" + ratings[9] );
			numOfCots.setText( ""+ ratings[8] );
			
		}
		
	}

	@Override
	public void actionPerformed( ActionEvent e ) {
		// TODO Auto-generated method stub

		if ( e.getSource() == vloMenuItem || e.getSource() == loMenuItem || e.getSource() == nomMenuItem ||
				e.getSource() == hiMenuItem || e.getSource() == vhiMenuItem )
		{
			ownerButton.setText( ((JMenuItem) e.getSource()).getText() );
		}
		
		else if ( e.getSource() == numOfCots )
			System.out.println(numOfCots.getText());
		
		else
		{
			
			ownerButton = ((JButton) e.getSource());
			// Default
			vloMenuItem.setEnabled( true );
			loMenuItem.setEnabled( true );
			nomMenuItem.setEnabled( true );
			hiMenuItem.setEnabled( true );
			vhiMenuItem.setEnabled( true );





			// Determine Which Popup to show ...
			if ( ownerButton.getText().endsWith( "%" ) )
			{
				incrementPopup.show( (Component) e.getSource() , ((JButton) e.getSource()).getWidth() / 2 , ((JButton) e.getSource()).getHeight() / 2 );
			}
			else
			{
				ratingPopup.show( (Component) e.getSource() , ((JButton) e.getSource()).getWidth() / 2 , ((JButton) e.getSource()).getHeight() / 2 );
			}
		}

	}


	public void getRatings( final Integer cot_id )
	{
		double ratings[] = new double[10];
		//int percent[] = new int[ 18 ];

		int counter = 0 ;
		
		//subSystem.setNumOfTailCots( Integer.parseInt( numOfCots.getText() ) );
		//subSystem.setTailMedianDefault( Double.parseDouble( medianTailEffort.getText() ));
		
		
		for ( int i = 0 ; i < ratingSetButtons.length ; i++ )
		{
			for ( int j = 0 ; j < ratingSetButtons[i].length ; j++ )
			{
				//ratings[counter++] = ((JButton) ratingSetButtons[i][j]).getText();
				ratings[counter++] = GlobalMethods.convertFromRating( (String)((JButton) ratingSetButtons[i][j]).getText() );
				//percent[counter++] = GlobalMethods.convertFromPercent( ((JButton) incrementSetButtons[i][j]).getText() );
			}
		}
		ratings[counter++] = Double.parseDouble( numOfCots.getText() );
		ratings[counter++] = Double.parseDouble( medianTailEffort.getText() );
		//subSystem.setCocotsRatings( cot_id , ratings);
		component.setTailCocotsRatings(cot_id, ratings);
	}
}
